﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGJHManage : Form
    {
        int iWorkMode = 0;
        SqlTools.SqlTools sqt;
        string strUserID;

        #region SQL脚本
        //获取当天最大的计划单号
        string strGetMaxPPNo = "select max(计划单号) 最大单号 from wgjha where 计划单号 like convert(varchar(100),getdate(),112)+'%'";
        //获取服务器日期
        string strGetServerDate = "select replace(CONVERT(varchar(12) , getdate(), 111),'/','-')";
        //按条件查询指定计划单单身
        string strSelectWGJHARange = "select * from wgjha where 计划单号 in (select distinct b.计划单号 from wgjha a,wgjhb b where a.计划单号=b.计划单号";
        //查询指定计划单单身
        string strSelectWGJHB = "select 计划项次,合同号,型号规格,WG型号规格,生产计划数量,WG计划数量,WG入库数量,备注 from wgjhb where 计划单号='@计划单号' order by 计划项次";
        //获取指定计划项次关联的生产计划项次
        string strGetSCJHXC = "select jhb024 合同号,生产计划号,生产计划项次,生产计划数量 from wgjhht,scjhb_table where 生产计划号=jhb001 and 生产计划项次=jhb002 and 计划单号='@计划单号' and 计划项次=@计划项次";
        //删除指定单号的单头
        string strDeleteWGJHA = "delete from wgjha where 计划单号='@计划单号'";
        //删除指定单号的单身
        string strDeleteWGJHB = "delete from wgjhb where 计划单号='@计划单号'";
        //删除指定单号的合同绑定
        string strDeleteWGJHHT = "delete from wgjhht where 计划单号='@计划单号'";
        //删除指定单号的申请绑定
        string strDeleteWGJHSQ = "delete from wgjhsq where 计划单号='@计划单号'";
        //插入单头
        string strInsertWGJHA = "insert into wgjha (计划单号) values ('@计划单号')";
        //插入单身
        string strInsertWGJHB = "insert into wgjhb (计划单号,计划项次,合同号,型号规格,WG型号规格,生产计划数量,WG计划数量,备注) values ('@计划单号',@计划项次,'@合同号','@型号规格','@WG型号规格',@生产计划数量,@WG计划数量,'@备注')";
        //插入合同关联
        string strInsertWGJHHT = "insert into wgjhht (计划单号,计划项次,生产计划号,生产计划项次,生产计划数量) values ('@计划单号',@计划项次,'@生产计划号','@生产计划项次',@生产计划数量)";
        //获取单据状态
        string strSelectWGJHMark = "select 单据状态 from wgjha where 计划单号='@计划单号'";
        //更新单据状态
        string strUpdateWGJHMark = "update wgjha set 单据状态=@单据状态 where 计划单号='@计划单号'";
        //更新提交时间
        string strUpdateWGJHSubmitDate = "update wgjha set 提交时间=@提交时间 where 计划单号='@计划单号'";
        //更新审核时间
        string strUpdateWGJHAuditDate = "update wgjha set 审核时间=@审核时间 where 计划单号='@计划单号'";
        //获取申请的合同关联
        string strSelectWGSQHT = "select jhb024 合同号,生产计划号,生产计划项次,生产计划数量 from wgsqht,scjhb_table where jhb001=生产计划号 and jhb002 =生产计划项次 and 申请单号='@申请单号' and 申请项次=@申请项次";
        //更新申请单项次审核
        string strUpdateWGSQAdudit = "update wgsqb set 已审核=@审核标记 where 申请单号='@申请单号' and 申请项次=@申请项次";
        //插入WG计划申请
        string strInsertWGJHSQ = "insert wgjhsq (计划单号,计划项次,申请单号,申请项次) values ('@计划单号',@计划项次,'@申请单号',@申请项次)";
        #endregion

        public frmWGJHManage(string strTmpUserID)
        {
            InitializeComponent();
            strUserID = strTmpUserID;
        }

        #region 通用事件
        private void frmWGJHManage_Load(object sender, EventArgs e)
        {
            cmbDocStatus.SelectedIndex = 0;
            ButtonMode(0);
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
        }

        private void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbSearchFields.Text.Length > 0 && txtSearchKeywords.Text.Trim().Length > 0)
            {
                if (lstKeywords.Items.Count > 0)
                {
                    for (int i = 0; i < lstKeywords.Items.Count; i++)
                    {
                        if (lstKeywords.Items[i].ToString().StartsWith(cmbSearchFields.Text))
                        {
                            lstKeywords.Items[i] = cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim();
                            return;
                        }
                    }
                }
                lstKeywords.Items.Add(cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim());
            }
        }

        private void lstKeywords_DoubleClick(object sender, EventArgs e)
        {
            if (lstKeywords.SelectedItem != null)
            {
                lstKeywords.Items.Remove(lstKeywords.SelectedItem);
            }
        }

        private void ButtonMode(int iTmpMode)
        {
            switch (iTmpMode)
            {
                case 0:      //一般工作状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = false;
                    btnCancel.Enabled = false;
                    btnGetData.Enabled = false;
                    btnGetSQ.Enabled = false;
                    //btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 1:      //添加状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false ;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = true ;
                    btnCancel.Enabled = true ;
                    btnGetData.Enabled = true ;
                    btnGetSQ.Enabled = true; 
                    //btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 2:      //编辑状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = true;
                    btnCancel.Enabled = true;
                    btnGetData.Enabled = true;
                    btnGetSQ.Enabled = true;
                    //btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                default :
                    clsTools.ShowWarningMsg(iTmpMode.ToString() + ":未设置的工作状态！");
                    break;
            }
        }

        private void FillRowIndex(DataGridView tmpdgv,string strTmpIDFieldName)
        {
            if (tmpdgv.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdgv.Rows.Count; i++)
                {
                    tmpdgv.Rows[i].Cells[strTmpIDFieldName].Value = i+1;
                }
            }
        }
        #endregion 通用事件

        private string GetPPNo()
        {
            string strTmpMaxPPNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxPPNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxPPNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxPPNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxPPNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh3"))
            {
                return;
            }
            iWorkMode = 1;                                                                //设置工作模式为新增模式
            if (dgv1.DataSource != null)
            {
                DataTable tmpdt = (DataTable)dgv1.DataSource;
                tmpdt.Rows.Add(tmpdt.NewRow());
            }
            else
            {
                dgv1.Rows.Add();
            }
            dgv1.Rows[dgv1.Rows.Count - 1].Cells["dgv1_c1"].Value = GetPPNo();
            dgv1.CurrentCell = dgv1.Rows[dgv1.Rows.Count - 1].Cells[0];
            dgv1.Enabled = false;
            lblMark.Text = "";
            lblMark.Visible = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(1);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            frmPPSelector newFrmPPSelector = new frmPPSelector();
            newFrmPPSelector.Owner = this.Owner;
            newFrmPPSelector.ShowDialog();
            if (newFrmPPSelector.bCancel || newFrmPPSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmPPSelector.dfrlst.Count > 0)
            {
                dgv2.Rows.Add(newFrmPPSelector.dfrlst.Count);
                for (int i = 0; i < newFrmPPSelector.dfrlst.Count; i++)
                {
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c2"].Value = newFrmPPSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c3"].Value = newFrmPPSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c4"].Value = newFrmPPSelector.dfrlst[i].iSum;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c5"].Value = newFrmPPSelector.dfrlst[i].iSum;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c7"].Value = newFrmPPSelector.dfrlst[i].strMemo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c8"].Value = newFrmPPSelector.dfrlst[i].strPPNo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c9"].Value = newFrmPPSelector.dfrlst[i].strPX;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c10"].Value = newFrmPPSelector.dfrlst[i].iQuantity;
                    dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c13"].Tag = newFrmPPSelector.dfrlst[i].strContract;
                    if (newFrmPPSelector.dfrlst[i].strContract.Length > 1)
                    {
                        dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c10"].Value = "多项";
                    }
                    else
                    {
                        dgv2.Rows[dgv2.Rows.Count - newFrmPPSelector.dfrlst.Count + i].Cells["dgv2_c10"].Value = newFrmPPSelector.dfrlst[i].strContract[0];
                    }
                }
                dgv2.AutoResizeColumns();
                FillRowIndex(dgv2,"dgv2_c1");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh2"))
            {
                return;
            }
            string strTmpSelectString = strSelectWGJHARange;
            for (int i = 0; i < lstKeywords.Items.Count; i++)
            {
                string strTmpFieldName = lstKeywords.Items[i].ToString().Split(':').GetValue(0).ToString();
                string strTmpKeyword = lstKeywords.Items[i].ToString().Split(':').GetValue(1).ToString();
                switch (strTmpFieldName)
                {
                    case "计划单号":
                        strTmpSelectString += " and b.计划单号 like '" + strTmpKeyword + "'";
                        break;
                    case "合同号":
                        strTmpSelectString += " and (select count(*) from scjhb_table,wgjhht where jhb001=生产计划号 and jhb002=生产计划项次 and wgjhht.计划单号=b.计划单号 and jhb024 like '" + strTmpKeyword + "')>0";
                        break;
                    case "型号规格":
                        strTmpSelectString += " and 型号规格 like '" + strTmpKeyword + "'";
                        break;
                    case "WG型号规格":
                        strTmpSelectString += " and WG型号规格 like '" + strTmpKeyword + "'";
                        break;
                    case "申请单号":
                        strTmpSelectString += " and (select count(*) from wgjhsq c where c.计划单号=b.计划单号 and c.申请单号 like '" + strTmpKeyword + "'";
                        break;
                }
            }
            if (cmbDocStatus.SelectedIndex != 0)
            {
                strTmpSelectString += " and 单据状态=" + Convert.ToString(cmbDocStatus.SelectedIndex-1);
            }
            strTmpSelectString += ")";
            dgv1 .DataSource = sqt.GetDataTable(strTmpSelectString);
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
            }
        }

        private void ReadData()
        {
            ButtonMode(0);
            if (dgv1.CurrentRow != null)
            {
                string strTmpJhNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                DataTable tmpdt = sqt.GetDataTable(strSelectWGJHB.Replace("@计划单号", strTmpJhNo));
                switch (clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c5"].Value).Trim())
                {
                    case "0":
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnDelete.Enabled = true;
                        btnSubmit.Enabled = true;
                        btnAudit.Enabled = false;
                        btnEdit.Enabled = true;
                        break;
                    case "1":
                        lblMark.Text = "提";
                        lblMark.Visible = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = true;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = true;
                        break;
                    case "2":
                        lblMark.Text = "核";
                        lblMark.Visible = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = false;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = false;
                        break;
                    case "3":
                        lblMark.Text = "完";
                        lblMark.Visible = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = false;
                        btnAudit.Enabled = false;
                        btnEdit.Enabled = false;
                        break;
                }
                dgv2.Rows.Clear();
                if (tmpdt.Rows.Count > 0)
                {
                    dgv2.Rows.Add(tmpdt.Rows.Count);
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        dgv2.Rows[i].Cells["dgv2_c1"].Value = tmpdt.Rows[i]["计划项次"];
                        dgv2.Rows[i].Cells["dgv2_c2"].Value = tmpdt.Rows[i]["型号规格"];
                        dgv2.Rows[i].Cells["dgv2_c3"].Value = tmpdt.Rows[i]["WG型号规格"];
                        string[] strTmpPP = new string[1];
                        string[] strTmpPX = new string[1];
                        int[] iTmpQuantity = new int[1];
                        string[] strTmpContractNo = new string[1];
                        DataTable tmpdtscjh = sqt.GetDataTable(strGetSCJHXC.Replace("@计划单号", strTmpJhNo)
                                                                                                                .Replace("@计划项次", tmpdt.Rows[i]["计划项次"].ToString()));
                        if (tmpdtscjh.Rows.Count > 0)
                        {
                            strTmpPP = new string[tmpdtscjh.Rows.Count];
                            strTmpPX = new string[tmpdtscjh.Rows.Count];
                            iTmpQuantity = new int[tmpdtscjh.Rows.Count];
                            strTmpContractNo = new string[tmpdtscjh.Rows.Count];
                            for (int k = 0; k < tmpdtscjh.Rows.Count; k++)
                            {
                                strTmpPP[k] = tmpdtscjh.Rows[k]["生产计划号"].ToString().Trim();
                                strTmpPX[k] = tmpdtscjh.Rows[k]["生产计划项次"].ToString().Trim();
                                iTmpQuantity[k] = clsTools.GetInt(tmpdtscjh.Rows[k]["生产计划数量"].ToString());
                                strTmpContractNo[k] = tmpdtscjh.Rows[k]["合同号"].ToString().Trim();
                            }
                        }
                        dgv2.Rows[i].Cells["dgv2_c4"].Value = tmpdt.Rows[i]["生产计划数量"];
                        dgv2.Rows[i].Cells["dgv2_c5"].Value = tmpdt.Rows[i]["WG计划数量"];
                        dgv2.Rows[i].Cells["dgv2_c6"].Value = tmpdt.Rows[i]["WG入库数量"];
                        dgv2.Rows[i].Cells["dgv2_c7"].Value = tmpdt.Rows[i]["备注"];
                        dgv2.Rows[i].Cells["dgv2_c8"].Value = strTmpPP;
                        dgv2.Rows[i].Cells["dgv2_c9"].Value = strTmpPX;
                        dgv2.Rows[i].Cells["dgv2_c10"].Value = iTmpQuantity;
                        dgv2.Rows[i].Cells["dgv2_c13"].Tag = strTmpContractNo;
                        if (strTmpContractNo.Length > 1)
                        {
                            dgv2.Rows[i].Cells["dgv2_c13"].Value = "多项";
                        }
                        else
                        {
                            dgv2.Rows[i].Cells["dgv2_c13"].Value = strTmpContractNo[0];
                        }
                    }
                    dgv2.AutoResizeColumns();
                }
            }
            else
            {
                dgv2.Rows.Clear();
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            ReadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh4"))
            {
                return;
            }
            iWorkMode = 2;                                                                //设置工作模式为编辑模式
            dgv1.Enabled = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(2);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh5"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpJhNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value).Trim();
                DialogResult dr = MessageBox.Show("确定删除单号为" + strTmpJhNo + "的计划单？删除以后将无法恢复！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery(strDeleteWGJHA.Replace("@计划单号", strTmpJhNo ));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGJHB.Replace("@计划单号", strTmpJhNo ));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGJHHT.Replace("@计划单号", strTmpJhNo));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGJHSQ.Replace("@计划单号", strTmpJhNo));
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
#region 清理空行
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
#endregion
#region 整理序号
                        FillRowIndex(dgv2,"dgv2_c1");
#endregion
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            //如果有采购型号规格和采购数量为空的，结束操作
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("采购型号规格和采购数量不允许为空！");
                                    return;
                                }
                            }
                            //获取新的计划单号
                            string strTmpJhNo = GetPPNo();
                            dgv1.CurrentRow .Cells ["dgv1_c1"].Value = strTmpJhNo ;
                            if (sqt.DoCmdExecuteNonQuery(strInsertWGJHA.Replace("@计划单号", strTmpJhNo )) > 0)
                            {
                                for (int i = 0; i < dgv2.Rows.Count; i++)
                                {
#region 插入外购计划合同
                                    if (dgv2.Rows[i].Cells["dgv2_c8"].Value != null && dgv2.Rows[i].Cells["dgv2_c9"].Value != null && dgv2.Rows[i].Cells["dgv2_c10"].Value != null)
                                    {
                                        string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                        string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c9"].Value;
                                        int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c10"].Value;
                                        for (int k = 0; k < strTmpPP.Length; k++)
                                        {
                                            if (strTmpPP[k].Trim().Length == 0)
                                            {
                                                continue;
                                            }
                                            sqt.DoCmdExecuteNonQuery(strInsertWGJHHT.Replace("@计划单号", strTmpJhNo )
                                                                                                                             .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                        }
                                    }
#endregion
#region 如果有申请单号的，变更申请单项次审核状态,插入外购计划申请
                                    if (dgv2.Rows[i].Cells["dgv2_c11"].Value != null && dgv2.Rows[i].Cells["dgv2_c12"].Value != null)
                                    {
                                        string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c11"].Value;
                                        string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c12"].Value;
                                        for (int k = 0; k < strTmpPP.Length; k++)
                                        {
                                            if (strTmpPP[k].Trim().Length == 0)
                                            {
                                                continue;
                                            }
                                            //插入外购计划申请
                                            sqt.DoCmdExecuteNonQuery(strInsertWGJHSQ.Replace("@计划单号", strTmpJhNo)
                                                                                                                     .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                     .Replace("@申请单号", strTmpPP[k].Trim())
                                                                                                                 .Replace("@申请项次", strTmpPX[k].Trim()));
                                        }
                                    }
#endregion
                                    sqt.DoCmdExecuteNonQuery(strInsertWGJHB.Replace("@计划单号", strTmpJhNo)
                                                                                                         .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                         .Replace("@合同号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c13"].Value))
                                                                                                         .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                        .Replace("@WG型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                        .Replace("@生产计划数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                        .Replace("@WG计划数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c7"].Value)));
                }
                            }
                            ButtonMode(0);
                            btnEdit.Enabled = true;
                            btnDelete.Enabled = true;
                            btnConfirm.Enabled = false;
                            btnSubmit.Enabled = true;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
                case 2:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value).Trim().Length == 0 || clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value).Trim().Length == 0)
                                {
                                    clsTools.ShowWarningMsg("采购型号规格和采购数量不允许为空！");
                                    return;
                                }
                            }
                            //删除原有信息
                            string strTmpJhNo=clsTools .GetString ( dgv1.CurrentRow .Cells ["dgv1_c1"].Value ).Trim ();
                            sqt.DoCmdExecuteNonQuery(strDeleteWGJHB.Replace("@计划单号", strTmpJhNo ));
                            sqt.DoCmdExecuteNonQuery(strDeleteWGJHHT.Replace("@计划单号", strTmpJhNo));
                            sqt.DoCmdExecuteNonQuery(strDeleteWGJHSQ.Replace("@计划单号", strTmpJhNo));
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                if (dgv2.Rows[i].Cells["dgv2_c8"].Value != null && dgv2.Rows[i].Cells["dgv2_c9"].Value != null && dgv2.Rows[i].Cells["dgv2_c10"].Value != null)
                                {
                                    string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c8"].Value;
                                    string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c9"].Value;
                                    int[] iTmpQuantity = (int[])dgv2.Rows[i].Cells["dgv2_c10"].Value;
                                    for (int k = 0; k < strTmpPP.Length; k++)
                                    {
                                        if (strTmpPP[k].Trim().Length == 0)
                                        {
                                            continue;
                                        }
                                        sqt.DoCmdExecuteNonQuery(strInsertWGJHHT.Replace("@计划单号", strTmpJhNo )
                                                                                                                             .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                             .Replace("@生产计划号", strTmpPP[k].Trim())
                                                                                                                             .Replace("@生产计划项次", strTmpPX[k].Trim())
                                                                                                                             .Replace("@生产计划数量", iTmpQuantity[k].ToString()));
                                    }
                                }
                                if (dgv2.Rows[i].Cells["dgv2_c11"].Value != null && dgv2.Rows[i].Cells["dgv2_c12"].Value != null)
                                {
                                    string[] strTmpPP = (string[])dgv2.Rows[i].Cells["dgv2_c11"].Value;
                                    string[] strTmpPX = (string[])dgv2.Rows[i].Cells["dgv2_c12"].Value;
                                    for (int k = 0; k < strTmpPP.Length; k++)
                                    {
                                        if (strTmpPP[k].Trim().Length == 0)
                                        {
                                            continue;
                                        }
                                        //插入外购计划申请
                                        sqt.DoCmdExecuteNonQuery(strInsertWGJHSQ.Replace("@计划单号", strTmpJhNo)
                                                                                                                 .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                 .Replace("@申请单号", strTmpPP[k].Trim())
                                                                                                             .Replace("@申请项次", strTmpPX[k].Trim()));
                                    }
                                }
                                sqt.DoCmdExecuteNonQuery(strInsertWGJHB.Replace("@计划单号", strTmpJhNo)
                                                                                                                         .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                         .Replace("@合同号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c13"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@WG型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@生产计划数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                                        .Replace("@WG计划数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c7"].Value)));
                            }
                            ButtonMode(0);
                            btnEdit .Enabled =true ;
                            btnDelete.Enabled =true ;
                            btnConfirm.Enabled = false ;
                            btnSubmit.Enabled = true;
                            dgv2.ReadOnly = true;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                    dgv2.Rows.Clear();
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
                case 2:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
            }
        }

        private void 添加行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgv2.Rows.Add();
            dgv2.CurrentCell = dgv2.Rows[dgv2.Rows.Count - 1].Cells[0];
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow != null)
            {
                dgv2.Rows.Remove(dgv2.CurrentRow);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh6"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpJhNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGJHMark.Replace("@计划单号", strTmpJhNo)));
                if (iMark == 0)
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@单据状态", "1"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHSubmitDate.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@提交时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                    for (int i = 0; i < dgv2.Rows.Count; i++)
                    {
                        if (dgv2.Rows[i].Cells["dgv2_c11"].Value != null)
                        {
                            string[] strPPNo = (string[])dgv2.Rows[i].Cells["dgv2_c11"].Value;
                            string[] strPX = (string[])dgv2.Rows[i].Cells["dgv2_c12"].Value;
                            for (int k = 0; k < strPPNo.Length; k++)
                            {
                                sqt.DoCmdExecuteNonQuery(strUpdateWGSQAdudit.Replace("@申请单号", strPPNo[k].Trim())
                                                                                                                    .Replace("@申请项次", strPX[k].Trim())
                                                                                                                    .Replace("@审核标记", "1"));
                            }
                        }
                    }
                    dgv1.CurrentRow.Cells["dgv1_c2"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                    lblMark.Text = "提";
                    lblMark.Visible = true;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnCancel.Enabled = false;
                    btnConfirm.Enabled = false;
                    btnAudit.Enabled = true;
                }
                else
                {
                    if (iMark == 1)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@单据状态", "0"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGJHSubmitDate.Replace("@计划单号", strTmpJhNo)
                                                                                                                                    .Replace("@提交时间", "null"));
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            if (dgv2.Rows[i].Cells["dgv2_c11"].Value != null)
                            {
                                string[] strPPNo = (string[])dgv2.Rows[i].Cells["dgv2_c11"].Value;
                                string[] strPX = (string[])dgv2.Rows[i].Cells["dgv2_c12"].Value;
                                for (int k = 0; k < strPPNo.Length; k++)
                                {
                                    sqt.DoCmdExecuteNonQuery(strUpdateWGSQAdudit.Replace("@申请单号", strPPNo[k].Trim())
                                                                                                                        .Replace("@申请项次", strPX[k].Trim())
                                                                                                                        .Replace("@审核标记", "0"));
                                }
                            }
                        }
                        dgv1.CurrentRow.Cells["dgv1_c2"].Value = string.Empty;
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                        btnCancel.Enabled = false;
                        btnConfirm.Enabled = false;
                        btnAudit.Enabled = false;
                    }
                }
            }
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "jh7"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpJhNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGJHMark.Replace("@计划单号", strTmpJhNo)));
                if (iMark == 1)
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@单据状态", "2"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGJHAuditDate.Replace("@计划单号", strTmpJhNo)
                                                                                                                                    .Replace("@审核时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                    dgv1.CurrentRow.Cells["dgv1_c3"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                    lblMark.Text = "核";
                    btnSubmit.Enabled = false;
                }
                else
                {
                    if (iMark == 2)
                    {
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            if (clsTools.GetInt(dgv2.Rows[i].Cells["dgv2_c6"].Value) > 0)
                            {
                                clsTools.ShowWarningMsg("第" + Convert.ToString(i + 1) + "项已有入库单！需删除所有相关入库单后才能反审核该单据！");
                                return;
                            }
                        }
                        sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@单据状态", "1"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGJHAuditDate.Replace("@计划单号", strTmpJhNo)
                                                                                                                    .Replace("@审核时间", "null"));
                        dgv1.CurrentRow.Cells["dgv1_c3"].Value = string.Empty;
                        lblMark.Text = "提";
                        btnSubmit.Enabled = true;
                    }
                }
            }
        }

        private void btnGetSQ_Click(object sender, EventArgs e)
        {
            frmSQSelector newFrmSQSelector = new frmSQSelector();
            newFrmSQSelector.Owner = this.Owner;
            newFrmSQSelector.ShowDialog();
            if (newFrmSQSelector.bCancel || newFrmSQSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmSQSelector.dfrlst.Count > 0)
            {
                dgv2.Rows.Add(newFrmSQSelector.dfrlst.Count);
                for (int i = 0; i < newFrmSQSelector.dfrlst.Count; i++)
                {
                    DataTable tmpdt = sqt.GetDataTable(strSelectWGSQHT.Replace("@申请单号", newFrmSQSelector.dfrlst[i].strPPNo[0])
                                                                                                .Replace("@申请项次", newFrmSQSelector.dfrlst[i].strPX[0]));
                    if (tmpdt.Rows.Count > 0)
                    {
                        string[] strPPNo = new string[tmpdt.Rows.Count];
                        string[] strPX = new string[tmpdt.Rows.Count];
                        int[] iQuantity = new int[tmpdt.Rows.Count];
                        string[] strContractNo = new string[tmpdt.Rows.Count];
                        for (int k = 0; k < tmpdt.Rows.Count; k++)
                        {
                            strPPNo[k] = clsTools.GetString(tmpdt.Rows[k]["生产计划号"]);
                            strPX[k] = clsTools.GetString(tmpdt.Rows[k]["生产计划项次"]);
                            iQuantity[k] = clsTools.GetInt(tmpdt.Rows[k]["生产计划数量"]);
                            strContractNo[k] = clsTools.GetString(tmpdt.Rows[k]["合同号"]);
                        }
                        dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c8"].Value = strPPNo;
                        dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c9"].Value = strPX;
                        dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c10"].Value = iQuantity;
                        dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c13"].Tag = strContractNo;
                        if (strContractNo.Length > 1)
                        {
                            dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c13"].Value = "多项";
                        }
                        else
                        {
                            dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c13"].Value = strContractNo[0];
                        }
                    }
                    else
                    {
                        dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c13"].Value = newFrmSQSelector .dfrlst [i].strContractNo;
                    }
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c2"].Value = newFrmSQSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c3"].Value = newFrmSQSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c4"].Value = newFrmSQSelector.dfrlst[i].iQuantity ;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c5"].Value = newFrmSQSelector.dfrlst[i].iQuantity ;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c7"].Value = newFrmSQSelector.dfrlst[i].strMemo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c11"].Value = newFrmSQSelector.dfrlst[i].strPPNo ;
                    dgv2.Rows[dgv2.Rows.Count - newFrmSQSelector.dfrlst.Count + i].Cells["dgv2_c12"].Value = newFrmSQSelector.dfrlst[i].strPX ;
                }
                dgv2.AutoResizeColumns();
                FillRowIndex(dgv2, "dgv2_c1");
            }
        }

        private void 标注预排ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.SelectedCells.Count > 0)
            {
                for (int i = 0; i < dgv2.SelectedCells.Count; i++)
                {
                    dgv2.Rows[dgv2.SelectedCells[i].RowIndex].Cells["dgv2_c13"].Value = "预排";
                }
            }
        }

        private void dgv2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 && dgv2.CurrentCell != null && dgv2.CurrentCell.Tag != null)
            {
                frmDetailViewer newFrmDetailViewer = new frmDetailViewer((string[])dgv2.CurrentCell.Tag);
                newFrmDetailViewer.ShowDialog();
            }
        }
    }
}